package com.example.f1.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.f1.domain.Product;
import com.example.f1.domain_param.ProductParam;
import com.example.f1.service.ProductService;
import org.example.dayu521.OrRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @ResponseBody
    @PostMapping("/list")
    public OrRes<?> list(@RequestBody ProductParam.Page p){
        var page = productService.lambdaQuery().page(new Page<>(Optional.ofNullable(p.getPage_num()).orElse(1l),
                Optional.ofNullable(p.getPage_size()).orElse(64l)));
        return OrRes.ok(page);
    }

    @ResponseBody
    @PostMapping("/list_by_ids")
    public OrRes<List<Product>> listbyid(@RequestBody Set<Long> ids){
        var list = productService.lambdaQuery().in(Product::getId,ids).list();
        return OrRes.ok(list);
    }

    @ResponseBody
    @PostMapping("/add")
    public OrRes<?> add(@RequestBody Product p){
        if(productService.save(p))
            return OrRes.ok();
        return OrRes.error("保存失败");
    }

    @ResponseBody
    @PostMapping("/update")
    public OrRes<?> update(@RequestBody Product p){
        if(productService.updateById(p))
            return OrRes.ok();
        return OrRes.error("保存失败");
    }
}
